package logic

import (
	"api/model/mysql"
	"context"
	"house_srv/house"
	"house_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *house.LoginRequest) (*house.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var login mysql.User

	mysql.DB.Debug().Where("user_name", in.UserName).Find(&login)

	if login.ID == 0 {
		user := mysql.User{
			UserName: in.UserName,
			PassWord: in.PassWord,
		}
		mysql.DB.Create(&user)
	} else {
		if login.PassWord != in.PassWord {
			logx.Error("密码错误")
		}
	}

	return &house.LoginResponse{
		Id: int64(login.ID),
	}, nil
}
